from django.db import models
from user_system.models import Project

# Create your models here.

class ConfSettingCenter(models.Model):
    """配置文件中心"""
    setting_name = models.CharField(max_length=128, blank=True, null=False, verbose_name="配置名称")
    server_id = models.IntegerField(blank=True, null=False, verbose_name="server_id")
    setting_content = models.TextField(blank=True, null=False, default="{}", verbose_name="配置内容")
    b_project = models.ForeignKey(to=Project, on_delete=models.CASCADE, verbose_name="属于哪个项目")
    purpose_choices = (
        (1, "游戏配置模板"),
        (2, "公共分区配置")
    )
    purpose = models.SmallIntegerField(choices=purpose_choices, default=1, verbose_name="配置用途")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.setting_name

    class Meta:
        verbose_name_plural = "配置文件中心"
        db_table = "confsettingcenter"

class GameServerGroup(models.Model):
    """游戏分区表"""
    group_name = models.CharField(max_length=64, blank=True, null=False, verbose_name="游戏分区")
    gameserver_number = models.IntegerField(blank=True, null=True, verbose_name="游戏数量")
    gameserver_number_not_son = models.IntegerField(blank=True, null=True, verbose_name="母服数量")
    gameserver_number_son = models.IntegerField(blank=True, null=True, verbose_name="子服数量")
    b_project = models.ForeignKey(to=Project, on_delete=models.CASCADE, verbose_name="属于哪个项目")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.group_name

    class Meta:
        verbose_name_plural = "游戏分区表"
        db_table = "gameservergroup"


class GameServer(models.Model):
    """游戏表"""
    server_dir = models.CharField(max_length=32, blank=True, null=False, verbose_name="游戏目录")
    server_name = models.CharField(max_length=32, blank=True, null=False, verbose_name="游戏名字")
    server_id = models.IntegerField(blank=True, null=False, verbose_name="server_id")
    b_project = models.ForeignKey(to=Project, on_delete=models.CASCADE, verbose_name="属于哪个项目")
    b_group = models.ForeignKey("GameServerGroup", on_delete=models.CASCADE, verbose_name="属于哪个分区")
    b_template = models.ForeignKey("ConfSettingCenter", null=True, blank=True, on_delete=models.SET_NULL, verbose_name="配置模板")
    server_ip = models.CharField(max_length=32, blank=True, null=False, verbose_name="游戏ip")
    ssh_port = models.IntegerField(blank=True, null=False, verbose_name="ssh端口")
    clean_status = models.CharField(max_length=10, blank=True, null=False, default=False,
                                    verbose_name="清档状态")  # False:未清档，True: 已清档
    clean_time = models.CharField(max_length=128, blank=True, null=True, verbose_name="清档时间")
    timing_task_status_choices = (
        (1, "未定时"),
        (2, "定时中"),
        (3, "执行成功"),
        (4, "执行失败"),
        (5, "执行中")
    )
    timing_task_status = models.SmallIntegerField(choices=timing_task_status_choices, default=1, verbose_name="定时任务状态")
    deploy_status_choices = (
        (1, "未部署"),
        (2, "已部署")
    )
    deploy_status = models.SmallIntegerField(choices=deploy_status_choices, default=2, verbose_name="部署状态")
    cross_status = models.CharField(max_length=10, blank=True, null=True, default=False,
                                    verbose_name="跨服状态")  # False:不是跨服，True: 是跨服
    is_son = models.CharField(max_length=10, blank=True, null=True, default=False,
                              verbose_name="是不是子服")  # False:不是子服，True: 是子服
    remarks = models.CharField(max_length=256, blank=True, null=True, verbose_name="备注")
    config_content = models.TextField(blank=True, null=False, default="{}", verbose_name="配置内容")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.server_name

    class Meta:
        verbose_name_plural = "游戏表"
        db_table = "gameserver"
